package com.hanyizhu.demostore.controller;

import com.hanyizhu.demostore.entity.Address;
import com.hanyizhu.demostore.service.IAddressService;
import com.hanyizhu.demostore.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户收货地址的实现层
 * @author lenovo
 */
@RestController
@RequestMapping("addresses")
public class AddressController extends BaseController{

    @Autowired
    private IAddressService addressService;
    @RequestMapping("add_new_address")
    public JsonResult<Void> addNewAddress(Address address, HttpSession session){
        Integer uid= getUidFromSession(session);
        String username = getUsernameFromSession(session);
        addressService.addNewAddress(address,uid,username);
        return new JsonResult<>(OK,ADDRESSSUCCESS);
    }

    @RequestMapping({"","/"})
    public JsonResult<List<Address>> getByUid(HttpSession session){
        Integer uid = getUidFromSession(session);
        List<Address> data = addressService.getByUid(uid);
        return new JsonResult<>(OK,data);
    }

    //@PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中：URL 中的 {xxx}
    // 占位符可以通过@PathVariable(“xxx“) 绑定到操作方法的入参中。
    @RequestMapping("{id}/set_default")
    public JsonResult<Void> setDefault(@PathVariable("id") Integer aid,HttpSession session){
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        addressService.setDefault(aid,uid,username);
        return new JsonResult<>(OK);
    }

}
